import { login, getUserInfo, logout } from "@/api/auth";
import { getToken, setToken, removeToken } from "@/utils/auth";

const state = {
	token: getToken(),
	userInfo: null,
};

const mutations = {
	SET_TOKEN: (state, token) => {
		state.token = token;
	},
	SET_USER_INFO: (state, userInfo) => {
		state.userInfo = userInfo;
	},
};

const actions = {
	// 用户登录
	login({ commit }, userInfo) {
		const { username, password } = userInfo;
		return new Promise((resolve, reject) => {
			login({ username: username.trim(), password })
				.then((response) => {
					const { data } = response;
					commit("SET_TOKEN", data.token);
					setToken(data.token);
					resolve();
				})
				.catch((error) => {
					reject(error);
				});
		});
	},

	// 获取用户信息
	getInfo({ commit, state }) {
		return new Promise((resolve, reject) => {
			getUserInfo()
				.then((response) => {
					const { data } = response;
					commit("SET_USER_INFO", data);
					resolve(data);
				})
				.catch((error) => {
					reject(error);
				});
		});
	},

	// 退出登录
	logout({ commit }) {
		return new Promise((resolve, reject) => {
			logout()
				.then(() => {
					commit("SET_TOKEN", "");
					commit("SET_USER_INFO", null);
					removeToken();
					resolve();
				})
				.catch((error) => {
					reject(error);
				});
		});
	},

	// 清除token
	resetToken({ commit }) {
		return new Promise((resolve) => {
			commit("SET_TOKEN", "");
			commit("SET_USER_INFO", null);
			removeToken();
			resolve();
		});
	},
};

export default {
	namespaced: true,
	state,
	mutations,
	actions,
};
